import Ele from '../ele/Ele'
import ForEle from '../ele/ForEle2'
import TextEle from '../ele/TextEle'
import FormEle from '../ele/FormEle'
import ForIfEle from '../ele/ForIfEle'
import ForTextEle from '../ele/ForTextEle'
import ForAttrEle from '../ele/ForAttrEle'
import ForFormEle from '../ele/ForFormEle'
import ForCompEle from '../ele/ForCompEle'
import EleTreeBuilder from 'EleTreeBuilder'
export default class ForEleTreeBuilder extends EleTreeBuilder{
    build(ele, okay, dest, index) {
        let childs = ele.ele.childNodes
        for(let child of childs) {
            if (child.nodeType == 3) {
                let s = child.nodeValue.trim()
                if (s == '') {
                    continue
                }
                let re0 = new RegExp('^[\s\S]*\{\{[^\}]+\}\}[\s\S]*');
                let re1 = new RegExp('^[\\s\\S]*\\{\\{\\s*(' + dest + ')[\\.]?\\w*\\}\\}[\\s\\S]*');
                if (re1.test(s)) {
                    ele.append(new ForTextEle(okay, child, dest, index));
                } else if (re0.test(s)) {
                    ele.append(new TextEle(okay, child));
                }
            }else if (child.tagName == 'FOR') {
                ele.append(new ForEle(okay, child, dest, index + 1))
            }else if (child.tagName == 'IF') {
                ele.append(new ForIfEle(okay, child, dest, index + 1))
            }else if (child.getAttribute('model')) {
                let model = child.getAttribute('model')
                let re = new RegExp('\\s*(' + dest + ')[\\.]?\\w*')
                if (re.test(model)) {
                    let fele = new ForFormEle(okay, child, dest, index)
                    ele.append(fele)
                    if (child.tagName == 'SELECT') {
                        this.build(fele, okay, dest, index)
                    }
                }else {
                    let fele = new FormEle(okay, child)
                    ele.append(fele)
                    if (child.tagName == 'SELECT') {
                        super.build(fele, okay)
                    }
                }
            }else if (child.tagName == 'COMP') {
                ele.append(new ForCompEle(okay,child, dest, index))
            }else {
                let s = child.outerHTML
                let re = new RegExp(/^<[a-zA-Z0-9]+\s+[^>]+\{\{[^\}{2}]+\}\}[^>]*>/);
                if(re.test(s)){
                    let ae = new ForAttrEle(okay,child,dest,index);
                    ele.append(ae);
                    this.build(ae,okay,dest,index);
                }else{
                    let e1 = new Ele(okay,child);
                    ele.append(e1);
                    this.build(e1,okay,dest,index);
                }
            }
        }
    }
}